package com.cadre.common.utils;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.cadre.common.exception.CadreException;
import com.cadre.common.utils.oldutil.ReportConfig;
import com.cadre.common.utils.oldutil.ResultEnum;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.*;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import java.io.File;

/* loaded from: Office2PDF.class */
public class Office2PDF {
    private static String OPEN_OFFICE_HOME = "D:/develop/OpenOffice 4/program";
    private static String OPEN_OFFICE_IP = "127.0.0.1";
    private static int OPEN_OFFICE_PORT = 8100;
    private static final Integer EXCEL_TO_PDF_OPERAND = 0;

    static {
        try {
            ComThread.InitSTA();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    public static void office2Pdf(String srcPath, String desPath, ReportConfig config) {
        File inputFile = new File(srcPath);
        if (inputFile.exists()) {
            File outputFile = new File(desPath);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().exists();
            }
            String command = config.getOpenOfficeHome() + "/soffice.exe -headless -accept=\"socket,host=" + config.getOpenOfficeIp() + ",port=" + config.getOpenOfficePort() + ";urp;\"";
            System.out.println("command:" + command);
            try {
                Process p = Runtime.getRuntime().exec(command);
                System.out.println("Process=>>>>>");
                OpenOfficeConnection connection = new SocketOpenOfficeConnection(config.getOpenOfficeIp(), config.getOpenOfficePort().intValue());
                connection.connect();
                System.out.println("OpenOfficeConnection=>>>>>");
                DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
                System.out.println("DocumentConverter=>>>>>");
                converter.convert(inputFile, outputFile);
                connection.disconnect();
                p.destroy();
            } catch (Exception e) {
                e.printStackTrace();
                throw new CadreException(ResultEnum.OPEN_OFFICE_ERROR);
            }
            //（已调整完毕）todo:需解决
        }
    }

    public static void excel2Pdf(String inFilePath, String outFilePath) throws Exception {
        Dispatch excel = null;
        ActiveXComponent ax = null;
        try {
            ax = null;
            excel = null;
            try {
                ax = new ActiveXComponent("Excel.Application");
                ax.setProperty("Visible", new Variant(true));
                ax.setProperty("AutomationSecurity", new Variant(3));
                excel = Dispatch.invoke(ax.getProperty("Workbooks").toDispatch(), "Open", 1, new Object[]{inFilePath, new Variant(false), new Variant(false)}, new int[9]).toDispatch();
                Dispatch.invoke(excel, "ExportAsFixedFormat", 1, new Object[]{new Variant(EXCEL_TO_PDF_OPERAND), outFilePath, new Variant(0)}, new int[1]);
                if (excel != null) {
                    Dispatch.call(excel, "Close", new Object[]{new Variant(false)});
                }
                if (ax != null) {
                    ax.invoke("Quit", new Variant[0]);
                }
                ComThread.Release();
            } catch (Exception es) {
                es.printStackTrace();
                throw es;
            }
        } catch (Throwable th) {
            if (excel != null) {
                Dispatch.call(excel, "Close", new Object[]{new Variant(false)});
            }
            if (ax != null) {
                ax.invoke("Quit", new Variant[0]);
            }
            ComThread.Release();
            throw th;
        }
    }

    /*  JADX ERROR: JadxRuntimeException in pass: BlockProcessor
        jadx.core.utils.exceptions.JadxRuntimeException: Unreachable block: B:15:0x00c7
        	at jadx.core.dex.visitors.blocks.BlockProcessor.checkForUnreachableBlocks(BlockProcessor.java:86)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.processBlocksTree(BlockProcessor.java:52)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.visit(BlockProcessor.java:44)
        */
    public static String addPageNum(String r6, String r7) {
        /*
        // Method dump skipped, instructions count: 252
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tdqy.cadre.modular.profess.utils.Office2PDF.addPageNum(java.lang.String, java.lang.String):java.lang.String");
    }

    /* loaded from: Office2PDF$PageNumPdfPageEvent.class */
    //（已调整完毕）todo: 报错需解决
    static class PageNumPdfPageEvent extends PdfPageEventHelper {
        PageNumPdfPageEvent() {
        }

        public void onEndPage(PdfWriter writer, Document document) {
            try {
                PdfContentByte pdfContent = writer.getDirectContent();
                pdfContent.saveState();
                pdfContent.beginText();
                BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
                Font fontDetail = new Font(baseFont, (float) 14, 0);
                pdfContent.setFontAndSize(baseFont, (float) 14);
                ColumnText.showTextAligned(pdfContent, 1, new Phrase(String.format("-%d-", Integer.valueOf(writer.getPageNumber())), fontDetail), (document.left() + document.right()) / 2.0f, document.bottom(-10.0f), 0.0f);
                pdfContent.endText();
                pdfContent.restoreState();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
